<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>带索引检查计算数组的差集</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.array-diff-assoc" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_diff_assoc</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.3.0, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">array_diff_assoc</span> &mdash; <span class="dc-title">带索引检查计算数组的差集</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.array-diff-assoc-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_diff_assoc</strong></span>
    ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array1</code></span>
   , <span class="methodparam"><span class="type">array</span> <code class="parameter">$array2</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$...</code></span>
  ] ) : <span class="type">array</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>array_diff_assoc()</strong></span> 返回一个数组，该数组包括了所有在
   <code class="parameter">array1</code> 中但是不在任何其它参数数组中的值。注意和
   <span class="function"><a href="array_diff.html" class="function">array_diff()</a></span> 不同的是键名也用于比较。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.array-diff-assoc-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>


    
     <dt>
<code class="parameter">array1</code></dt>

     <dd>

      <p class="para">
       从这个数组进行比较
      </p>
     </dd>

    

    
     <dt>
<code class="parameter">array2</code></dt>

     <dd>

      <p class="para">
       被比较的数组
      </p>
     </dd>

    

    
     <dt>
<code class="parameter">...</code></dt>

     <dd>

      <p class="para">
       更多被比较的数组
      </p>
     </dd>

    

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-diff-assoc-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns an <span class="type"><a href="language.types.array.html" class="type array">array</a></span> containing all the values from
   <code class="parameter">array1</code> that are not present in any of the other arrays.
  </p>
 </div>




 <div class="refsect1 examples" id="refsect1-function.array-diff-assoc-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-6019">
    <p><strong>Example #1 <span class="function"><strong>array_diff_assoc()</strong></span> 例子</strong></p>
    <div class="example-contents"><p>
   上面的例子中可以看到键值对 <em>&quot;a&quot; =&gt; &quot;green&quot;</em>
   在两个数组中都有，因此不在本函数的输出中。与此不同，键值对
   <em>0 =&gt; &quot;red&quot;</em> 出现在输出中是因为第二个参数中的
   <em>&quot;red&quot;</em> 的键名是 <em>1</em>。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array1&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"brown"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"c"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"blue"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"red"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$array2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"yellow"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"red"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_diff_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">$array1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [b] =&gt; brown
    [c] =&gt; blue
    [0] =&gt; red
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-6020">
    <p><strong>Example #2 <span class="function"><strong>array_diff_assoc()</strong></span> example</strong></p>
    <div class="example-contents"><p>
     键值对 <em class="emphasis">key =&gt; value</em>
   中的两个值仅在 <em>(string) $elem1 === (string)
   $elem2 </em> 时被认为相等。也就是说使用了严格检查，字符串的表达必须相同。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$array1&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$array2&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"00"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"01"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"2"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_diff_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">$array1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$array2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [0] =&gt; 0
    [1] =&gt; 1
    )
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.array-diff-assoc-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    注意本函数只检查了多维数组中的一维。当然可以用
    <em>array_diff_assoc($array1[0], $array2[0]);</em>
    检查更深的维度。
   </span>
  </p></blockquote>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    使用更多的键比较相似数组时，确保你传入参数的顺序是正确的。
    新的数组应该是在列表里的第一个。
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.array-diff-assoc-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="array_diff.html" class="function" rel="rdfs-seeAlso">array_diff()</a> - 计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="array_diff_uassoc.html" class="function" rel="rdfs-seeAlso">array_diff_uassoc()</a> - 用用户提供的回调函数做索引检查来计算数组的差集</span></li>
    <li class="member"><span class="function"><a href="array_udiff_assoc.html" class="function" rel="rdfs-seeAlso">array_udiff_assoc()</a> - 带索引检查计算数组的差集，用回调函数比较数据</span></li>
    <li class="member"><span class="function"><a href="array_udiff_uassoc.html" class="function" rel="rdfs-seeAlso">array_udiff_uassoc()</a> - 带索引检查计算数组的差集，用回调函数比较数据和索引</span></li>
    <li class="member"><span class="function"><a href="array_intersect.html" class="function" rel="rdfs-seeAlso">array_intersect()</a> - 计算数组的交集</span></li>
    <li class="member"><span class="function"><a href="array_intersect_assoc.html" class="function" rel="rdfs-seeAlso">array_intersect_assoc()</a> - 带索引检查计算数组的交集</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>